package servlet.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * @Author Administrator
 * @Date 2020/5/14 15:45
 **/
@WebServlet("/requestDemo1")
public class RequestDemo1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*req:请求对象request;resp:响应对象response
        获取请求消息
        1.获取请求路径url
        获取Servlet路径（别名）：getServletPath()
        URL(统一资源定位符）：完整的访问路径：getRequestURL()
        URI(项目虚拟路径+servlet路径)：getRequestURI（）
         */
        String contextPath = req.getContextPath();
        String servletPath = req.getServletPath();
        StringBuffer requestURL = req.getRequestURL();
        String requestURI = req.getRequestURI();
        System.out.println(contextPath);
        System.out.println(servletPath);
        System.out.println(requestURL);
        System.out.println(requestURI);

        String method = req.getMethod();
        System.out.println(method);
        /*
        获取请求头数据
         */
        String agent=req.getHeader("user-Agent");
        System.out.println(agent);
        /*判断是哪个浏览器来浏览的*/
        if (agent.contains("Chrome")){
            System.out.println("谷歌Chrome");
        }else if (agent.contains("Firefox")){
            System.out.println("火狐浏览器");
        }else {
            System.out.println("其他浏览器");
        }


    }
}